package cn.edu.xjtu.nbrash.xbarsuite.terry;

//import com.google.zxing.integration.android.IntentIntegrator;

import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;

import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.R.layout;
import cn.edu.xjtu.nbrash.xbarsuite.R.menu;
import cn.edu.xjtu.nbrash.xbarsuite.zxingdemo.RashDisplayActivity;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.Intent;
import android.text.InputFilter;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EncodeEmail extends Activity {
	
//	final EditText emailEdit = (EditText) findViewById(R.id.email_text);


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_encode_email);

		final EditText emailEdit = (EditText) findViewById(R.id.email_text);
		final EditText themeEdit = (EditText) findViewById(R.id.theme_text);

		emailEdit.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
		themeEdit.setFilters(new InputFilter[] { new InputFilter.LengthFilter(20) });

	}

	public void showQR(View v) {
			final EditText emailEdit = (EditText) findViewById(R.id.email_text);
			final EditText themeEdit = (EditText) findViewById(R.id.theme_text);

			if(!isEmail(emailEdit.getText().toString())){
				
				
					Toast.makeText(this, "请输入正确的email地址！", Toast.LENGTH_LONG).show();
			}
			else if(themeEdit.getText().toString().length()==0){
				Toast.makeText(this, "请输入主题！", Toast.LENGTH_LONG).show();
			}
			
			else{
				String mailTo = ((TextView) findViewById(R.id.email_text)).getText()
						.toString();
				String theme = ((TextView) findViewById(R.id.theme_text)).getText()
						.toString();
				String content = ((TextView) findViewById(R.id.content_Text)).getText()
						.toString();
	
				String data = mailTo + "?" + "subject=" + theme + "&body=" + content;
	
				if (mailTo != null && !mailTo.equals("")) {
					Intent intent = new Intent(this, RashDisplayActivity.class);
					// ACTION 是 ENCODE
					intent.setAction(Intents.Encode.ACTION);
					// 类型是TEXT，这里可以有很多中类型，具体参见QRCodeEncoder源码
					intent.putExtra(Intents.Encode.TYPE, Contents.Type.EMAIL);
					// 数据，这里是字符串
					intent.putExtra(Intents.Encode.DATA, data);
					startActivity(intent);

			}
			
		}
	}

	public void back(View v) {
		finish();
	}
	
	public boolean isEmail(String str){
		

		if(str.matches("^\\w+@\\w+\\.(com|cn)")) return true;
//		if(str.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+")) return true;
				   
		return false;
}
	// public void showQR(View v) {
	// String mailTo = ((TextView)
	// findViewById(R.id.email_text)).getText().toString();
	// String theme = ((TextView)
	// findViewById(R.id.theme_text)).getText().toString();
	// String content = ((TextView)
	// findViewById(R.id.content_Text)).getText().toString();
	//
	// if(mailTo != null && !mailTo.equals("")){
	// Intent intent = new Intent(this, RashDisplayActivity.class);
	// Bundle bundle = new Bundle();
	//
	// // ACTION 是 ENCODE
	// intent.setAction(Intents.Encode.ACTION);
	// // 类型是TEXT，这里可以有很多中类型，具体参见QRCodeEncoder源码
	// intent.putExtra(Intents.Encode.TYPE, Contents.Type.EMAIL_ALL);
	// // 数据，这里是字符串
	// bundle.putString(SomeNewConstant.MAILTO, mailTo);
	// bundle.putString(SomeNewConstant.TITLE, theme);
	// bundle.putString(SomeNewConstant.CONTENT, content);
	//
	// intent.putExtra(Intents.Encode.DATA,bundle);
	// startActivity(intent);
	// }
	// }

}
